#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "echiquier.h"
#include "pieces.h"
#include "deplace.h"
#include "sdl.h"


int main ()
{
    struct s_echiquier            e;
    struct s_case *case_init;
    struct s_case *case_final;
    struct s_case *case_init_prec = NULL; /*case initiale precedente*/
    size_t i,j;
    struct s_deplace coup_pos[60];
    SDL_Event event;
    int continuer = 1;
    enum color joueur = blanc;
    /* sert a savoir si on click ou non */
    int click = 0;

    e = echiquier_create();
    echiquier_init ( &e );

    /* code de flo */
    init_sdl(&e);
    /* init de la place des pieces*/
    for (i = 0; i < 8;i++)
      for (j = 0; j < 2;j++)
	    init_piece_sdl (&e, j, i);
    for (i = 0; i < 8;i++)
      for (j = 7; j > 5;j--)
        init_piece_sdl (&e, j, i);

    ech_print( &e );

/* tant quon ferme pas sdl */
while(continuer)
      {
      /* attente d un evt */
	SDL_WaitEvent(&event);
    /* depend de l evt*/
	switch(event.type)
	  {
      /* si on quite... ben on quite la boucle*/
	  case SDL_QUIT:
	    continuer = 0;
	    break;
        /* si on click down */
	  case SDL_MOUSEBUTTONDOWN:
	    if (!click)
	      {
          if (!case_init_prec)
		  case_init_prec = case_init;
		printf("case_init_prec : %i\n",case_init->num);
		case_init = check_case_sdl(event.button.x, event.button.y, &e);
		if (!case_init)
		  {
		    printf("case null\n");
		    decolo_sdl(&e);
		  }
		else
		  {
		    printf("Pion sur la case ? %i, %c soit %i\n",case_init->num, case_init->let,((int)case_init->let -65));
		    if (case_init->p)
		      {
			printf("piece presente\n");
			if (joueur == case_init->p->piece_color)
			  {
			    printf("piece blanche\n");
			    decolo_sdl(&e);
			    deplace_pos_possible(&e, case_init->num,
						 (int)case_init->let -65,coup_pos);
			    colo_sdl(&e, coup_pos);
			  }
			else
			  case_init = NULL;
		      }
		      
		  }
		click = 1;
	      }
	    break;

        /*cas du relachent du click*/
	  case SDL_MOUSEBUTTONUP:
      printf("cilick : %d\n",click);
	    if (click)
	      {
		click = 0;
		case_final = check_case_sdl(event.button.x, event.button.y, &e);
		if ((case_init != NULL)&& (case_final != NULL))
		  {
		    printf("1\n");
		    if (case_init != case_final)
		      {
			printf("2\n");
			if (!case_final->bleu)
			  {
			    printf("3\n");
			    decolo_sdl(&e);
			  }
			else
			  {
			    printf("Case bleue\n");
			    if (case_init_prec == NULL)
			      {
				deplacement(&e, case_init->num, ((int)case_init->let)-65,
					    case_final->num, ((int)case_init->let)-65);
				change_postion(&e, case_init->num, ((int)case_init->let)-65);
				change_postion(&e, case_final->num, ((int)case_final->let)-65);
			      }
			    else
			      {
				printf("%i\n",case_init_prec->num);
				deplacement(&e, case_init_prec->num, ((int)case_init_prec->let)-65, case_final->num, ((int)case_final->let)-65);
                                change_postion(&e, case_init_prec->num, ((int)case_init_prec->let)-65);
                                change_postion(&e, case_final->num, ((int)case_final->let)-65);

			      }
			    printf("WTF ?\n");
			    decolo_sdl(&e);
			    case_init = NULL;
			  }
		      }
		    else
		      /* si on est sur une case bleu go sion pas clean */
		      if (case_final->bleu)
			{
			  printf("Case bleue\n");
			  deplacement(&e, case_init->num, ((int)case_init->let)-65,
				      case_final->num, ((int)case_init->let)-65);
			  /*deplacement_sdl;*/
			  printf("WTF ?\n");
			  if (joueur == blanc)
			    joueur = noir;
			  else
			    joueur = blanc;
			  decolo_sdl(&e);
			}
		  }
	      }
	  }
      }
    free_sdl(&e);
   
    return 0;
}
